package com.xpgk.mvc.bsendMessage;


import java.util.List;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.template.expr.ast.Index;
import com.xpgk.dto.AjaxRender;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.bentryRegister.Customer;
import com.xpgk.mvc.messageModel.ShortMessage;
import com.xpgk.mvc.organization.Organization;
import com.xpgk.mvc.productOutin.ProductOutin;
import com.xpgk.mvc.user.User;

public class BsendMessageController extends  BaseController{

	public void index(){
		setAttr("curOrgan", Organization.dao.findById(getCUserOrganIds()));
		render("BsendMessage.html");
	}
	/**
	 * 顾客列表
	 */
	public void view(){
		String json = paging(splitPage, BsendMessageSQL.sqlId_splitPageSelect, BsendMessageSQL.sqlId_splitPageFrom);
		renderJson(json);		
	}
	
	/**
	 * 店员列表
	 */
	public void viewEmployee(){
		String json = paging(splitPage,BsendMessageSQL.sqlId_splitPageSelectEmployee, BsendMessageSQL.sqlId_splitPageFromEmployee);
		renderJson(json);		
	}
	
	/**
	 * 短信模板查询
	 */
	public void messageModel(){
		ShortMessage shortMessage = getModel(ShortMessage.class);
		String sql = "select * from shortmessagemodel where status='启用' ";
		List<ShortMessage> find = shortMessage.dao().find(sql);
		renderJson(find);		
	}
	
	
	/*
	 * 顾客短信发送
	 */
	@Before(Tx.class)
	public void message(){
		List<BsendMessage> poList = getModels(BsendMessage.class);
		for(BsendMessage p : poList){
			p.save();
		}
		String phone = "18234083837";
		String regCode = StringUtils.randNum(6); //六位验证码 可以删除
		setSessionAttr("regCode", regCode); //六位验证码 可以删除
		String content = SMSAction.getCode(regCode);  //短信模板，待选
		String ip = IPUtil.getIpAddr(getRequest()); //客户机ip地址
		new Thread(new SMSAction(phone, content, ip)).start();
		//((Runnable) SMSAction.getJob(phone, content, ip)).run();
		render(AjaxRender.success("短信已发送"));
		render("BsendMessage.html");
	}
	/*
	 * 查询顾客短信发送
	 */
	public void queryMessage(){
		String ids = getPara("ids");
		setAttr("ids",ids);
		render("BsendMessage2.html");
	}
	/*
	 * 查询顾客短信发送数据
	 */
	public void queryMessagedata(){
		String ids = getPara("ids");
		String sql = "select * from sb_bsendmessage where position='2' and peopleids=?";
		List<Record> find = Db.find(sql,ids);
		renderJson(find);
	}
	/*
	 * 查询店员短信发送
	 */
	public void queryMessageEmployee(){
		String ids = getPara("ids");
		setAttr("ids",ids);
		render("BsendMessage3.html");
	}
	/*
	 * 查询店员短信发送数据
	 */
	public void queryMessageEmployeedata(){
		String ids = getPara("ids");
		String sql = "select * from sb_bsendmessage where position='1' and peopleids=?";
		List<Record> findEmployee = Db.find(sql,ids);
		renderJson(findEmployee);
	}
	
	/*
	 * 店员短信发送
	 */
	@Before(Tx.class)
	public void messageEmployee(){
		System.out.println(123);
		List<BsendMessage> poList = getModels(BsendMessage.class);
		for(BsendMessage p : poList){
			System.out.println(p);
		}
		for(BsendMessage p : poList){
			p.save();
		}
		String phone = "18234083837";
		String regCode = StringUtils.randNum(6); //六位验证码 可以删除
		setSessionAttr("regCode", regCode); //六位验证码 可以删除
		String content = SMSAction.getCode(regCode);  //短信模板，待选
		String ip = IPUtil.getIpAddr(getRequest()); //客户机ip地址
		new Thread(new SMSAction(phone, content, ip)).start();
		render(AjaxRender.success("短信已发送"));
		render("BsendMessage.html");
	}
}